load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("//tools/lint:lint.bzl", "add_lint_tests")
load("//tools/skylark:drake_py.bzl", "drake_py_binary")

drake_py_binary(
    name = "upgrade",
    srcs = ["upgrade.py"],
    data = [
        "@openusd_internal//:cmake_macros",
        "@openusd_internal//:cmakelists",
    ],
    tags = [
        # Only build this binary when necessary for linting or re-locking.
        "manual",
    ],
    deps = [
        "@rules_python//python/runfiles",
    ],
)

genrule(
    name = "_gen_files",
    outs = ["gen/files.bzl"],
    cmd = "$(execpath :upgrade) --output=$@",
    tags = [
        # Only compute the new lockfile when necessary for linting.
        "manual",
    ],
    tools = [":upgrade"],
)

diff_test(
    name = "lock_test",
    failure_message = (
        "To fix, run this command:\n" +
        "  bazel run //tools/workspace/openusd_internal:upgrade -- --relock"
    ),
    file1 = ":lock/files.bzl",
    file2 = select({
        "//tools:with_usd": ":gen/files.bzl",
        # When USD is off, we nerf this test to be a self-comparison no-op.
        "//conditions:default": ":lock/files.bzl",
    }),
    tags = [
        "lint",
    ],
)

add_lint_tests()
